package cn.itcast.babasport.controller.upload;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.itcast.babasport.service.upload.UploadService;
import cn.itcast.babasport.utils.constants.BbsConstants;

@Controller
@RequestMapping("/upload")
public class UploadController {
	@Resource
	private UploadService uploadService;
	
	/*@RequestMapping("/uploadPic.do")
	public void uploadPic(HttpServletRequest request, HttpServletResponse response, MultipartFile pic) throws Exception{
		if (pic != null && pic.getSize()>0) {
			//修改附件上传的名称
			String filename = pic.getOriginalFilename();
			//获得文件的扩展名
			String suffix = FilenameUtils.getExtension(filename);
			//生成UUID字符串
			String uuid = UUID.randomUUID().toString().replace("-", "");
			//拼接成新的文件名称
			String newName = uuid +"."+suffix;
			
			//附件上传
			String realPath = request.getServletContext().getRealPath("");
			String allUrl = "\\upload\\"+newName;
			String path = realPath+allUrl;
			File file = new File(path);
			pic.transferTo(file);
			
			
			//相应
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("allUrl", allUrl);
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(jsonObject.toString());
			
		}
		
	}*/
	
	@RequestMapping("uploadPic.do")
	public void uploadPic(HttpServletRequest request,HttpServletResponse response,MultipartFile  pic) throws Exception{
		if (pic != null && pic.getSize()>0) {
			String path = uploadService.uploadPicToFastDFS(pic.getBytes(), pic.getOriginalFilename());
			String allUrl = "http://192.168.200.128/"+path;
			
			//响应
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("allUrl", allUrl);
			jsonObject.put("imgUrl", path);
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(jsonObject.toString());
			
		}
		
	}
	
	/**
	 * 添加多张图片
	 * @param model
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@RequestMapping("/uploadPics.do")
	@ResponseBody
	public List<String> uploadPics(@RequestParam MultipartFile[] pics) throws Exception{
		List<String> urls = new ArrayList<>();
		for (MultipartFile pic : pics) {
			//将每一张图片上传到fastDFS上
			String path = uploadService.uploadPicToFastDFS(pic.getBytes(), pic.getOriginalFilename());
			//拼接服务器地址
			String url = BbsConstants.IMG_URL+path;
			//将路径添加到集合中
			urls.add(url);
		}
		return urls;
	}
	
}
